<?php
namespace App\Http\Controllers;

use App\Http\Models\Space;
use App\Http\Models\SpaceOrder;
use App\Http\Models\SpaceOrderDetail;
use App\Http\Models\User;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\View;
use Illuminate\Http\Request;
use Redirect;
use Illuminate\Support\Facades\Session;

class SpaceController extends MessageController
{
    public $modelName = 'space';

    public function getIndex()
    {
        $today = date('Y-m-d', time());

        //echo $datas;
        return View::make($this->modelName . "/list", compact('today'));
    }

    public function postIndex(Request $request)
    {
        $data = $request->all();

        $data['amount']=User::Auth()->CAmount;

        if($data['price']>$data['amount']){
            return Redirect::to('member/index')->withError(("预订需要花费￥".$data['price']."。余额不足！请充值后预订！"));
        }

        if (SpaceOrder::doCreate($data)) {
            return Redirect::to($this->modelName . '/index')->withSuccess("预定成功");
        } else {
            return Redirect::to($this->modelName . '/index')->withError("您预定的时间已被预定，预定失败，请重新操作");
        }
    }

    public function getMsgs(Request $request)
    {
        $data = $request->all();
        $model = new Space();
        $datas = $model->getDatas($data['date'], $data['type'], $data['SSiteID'], $data['basketball'], $data['soccer']);
        return $datas;
    }

    public function getDetail($data)
    {
        if ($data->SOCustomerID !== Session::get('customer_id')) {
            die('Access Denied!');
        }
        $details = SpaceOrderDetail::where('TODOID', $data->SOID)->leftJoin('tspace', 'tspace.SID', '=', 'tspaceorderdetail.TODSpaceID')->select('tspace.SName','tspace.SSiteID', 'tspaceorderdetail.*')->get();
        return View::make($this->modelName . "/detail", compact('data', 'details'));
    }

}
